<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $user_id
 * @property string $username
 * @property string $email
 * @property string $password
 * @property string $createtime
 * @property string $activationKey
 * @property string $role
 *
 * The followings are the available model relations:
 */
class User extends CActiveRecord
{
    public $password1;
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, email, password, password1', 'required'),
			
			// magic is out there
            array('email',    'match',   'pattern'    => '/^([a-z0-9_\.-]+)@([a-z0-9_\.-]+)\.([a-z\.]{2,6})$/', 'message' => 'Не верный формат e-mail адреса.'),
            array('username', 'match',   'pattern'    => '/^[A-Za-z0-9_-А-Яа-я\s,]+$/u','message'  => 'Логин содержит недопустимые символы.'),
            array('password1', 'compare', 'compareAttribute' => 'password'),
            
			array('username, email', 'length', 'max'=>100),
			array('activationKey', 'length', 'max'=>32),
			array('password, password1', 'length',  'max' => 40,  'min' => 5,),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, username, email, password, createtime, activationKey, role', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		'Project'=>array(self::HAS_MANY,'Project','user_id'),
		'Projectgroup'=>array(self::HAS_MANY,'Projectgroup','project_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'username' => 'Username',
			'email' => 'Email',
			'password' => 'Password',
			
			'createtime' => 'Createtime',
			'activationKey' => 'Activation Key',
			'role' => 'Role',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('createtime',$this->createtime,true);
		$criteria->compare('activationKey',$this->activationKey,true);
		$criteria->compare('role',$this->role,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}
